package com.r_z.mapper;

import com.r_z.entity.Outbound;
import com.r_z.entity.OutboundDetails;
import com.r_z.entity.Repertory;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 出库操作的mapper层
 * @author 任俊杰
 * @version 1.0
 * @date 2023/5/29
 */
@Mapper
public interface OutboundMapper {
    /**
     * 查询出库信息
     *@param where 条件
     *@param page 页码
     *@param limit 行数
     *@return 出库数据
     */
@Select("SELECT so.`salesOrderId`,o.`nowTime`,o.`totalNum`,o.`state` FROM outbound o JOIN salesOrder so ON so.`salesOrderId`=o.`salesOrderId` where 1=1  ${where} limit #{page},#{limit}")
List<Outbound>selectOutbound(@Param("page") Integer page,@Param("limit") Integer limit,@Param("where") String where);

    /**
     * 查询条数
     * @param where 条件
     * paymentState=2 付款状态 2为未支付
     * @return 行数
     */
    @Select("select count(*) from salesOrder o where 1=1 and paymentState=2 ${where}")
    int getTotal(@Param("where") String where);

    /**
     * 查询出库详情
     * @param salesOrderId 销售id
     * @return 数据详情
     */
    @Select("SELECT p.productId, p.productName, p.price,p.productNum as quantity FROM orderDetails od JOIN product p ON od.productId=p.productId WHERE od.salesOrderId=#{salesOrderId}")
    OutboundDetails selectOutboundDetails(@Param("salesOrderId") String salesOrderId);

    /**
     * 增加出库
     * @param outbound 出库实体
     * @return 返回一个标识 i>=1则成功 反之失败
     */
    @Insert("insert into outbound(outboundId,totalNum,nowTime,userId,state,salesOrderId)" +
            " values (#{outboundId},#{totalNum},#{nowTime},#{userId},#{state},#{salesOrderId})")
    int addOutbound(Outbound outbound);

    /**
     * 增加出库详情
     * @param outboundDetails 出库详情实体
     * @return 返回一个标识 i>=1则成功 反之失败
     */
    @Insert("insert into outboundDetails(outboundId,productId,productName,quantity) " +
            "values(#{outboundId},#{productId},#{productName},#{quantity})")
    int addOutboundDetails(OutboundDetails outboundDetails);

    /**
     * 修改销售出库状态
     * @param salesOrderId 销售id
     * @return 返回一个标识 i>=1则成功 反之失败
     */
    @Update("update salesOrder set deliveryStatus=1 where salesOrderId=#{salesOrderId}")
    int updateOutbound(@Param("salesOrderId") String salesOrderId);

    /**
     * 修改库存的出库状态
     * @return
     */
    @Update("UPDATE outbound SET state=1 WHERE salesOrderId=#{salesOrderId}")
    int updateOutbound2(@Param("salesOrderId") String salesOrderId);


    /**
     * 查询库存的数量
     * @return 返回一个标识 受影响的行数
     */
    @Select("select * from repertory where productId=#{productId}")
    public Repertory selectRepertoryCount(int productId);

    /**
     * 改库存的数量
     * @param quantity 数量
     * @param productId 商品id
     * @return 返回一个标识 受影响的行数
     */
    @Update("update repertory set quantity=quantity-#{quantity} where productId=#{productId}")
    public int updateOutboundCount(@Param("quantity") int quantity,@Param("productId")int productId);
}
